package com.xhh.hybriddemo.base;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


import com.xhh.hybriddemo.R;
import com.xhh.hybriddemo.common.AppContext;

import org.apache.cordova.CordovaActivity;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 界面基类
 */
public abstract class BaseActivity extends CordovaActivity  {

    protected Toolbar mToolbar;
    private TextView mCustomTitleTextView;
    private Unbinder unbinder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            //设置状态栏颜色
         //   window.setStatusBarColor(getResources().getColor(R.color.red_fd5744));
        }
        super.onCreate(savedInstanceState);
        initBefore(savedInstanceState);
        if (getContentViewId() <= 0) {
            throw new RuntimeException("layout resId undefind");
        }
        this.setContentView(this.getContentViewId());
        unbinder = ButterKnife.bind(this);
        init();
    }

    protected abstract int getContentViewId();

    // 初始化之前
    protected void initBefore(Bundle savedInstanceState) {
        AppContext.getInstance().addActivity(this.getClass(), this);
    }

    protected abstract void init();


    @Override
    protected void onTitleChanged(CharSequence title, int color) {
        //super.onTitleChanged("", color);
        if (mCustomTitleTextView != null && title != null) {
            mCustomTitleTextView.setText(title);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (getOptionsMenuId() != -1) {
            getMenuInflater().inflate(getOptionsMenuId(), menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 当前页菜单资源
     *
     * @return
     */
    protected int getOptionsMenuId() {
        return -1;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        AppContext.getInstance().removeActivity(this.getClass());
    }

    /**
     * home返回键
     */
    protected void onHomeClick() {
        finish();
    }

}